package pl.mchaniewski.ryneczek.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.mchaniewski.ryneczek.dao.IndexDao;
import pl.mchaniewski.ryneczek.pojo.Index;

@Repository("indexDao")
@Transactional(readOnly = true)
public class HibernateIndexDao extends HibernateGenericDao<Index, Integer>
        implements IndexDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Index> loadAll() {
        String queryString = "from Index";
        Query query = getEntityManager().createQuery(queryString);

        return query.getResultList();
    }

    @Override
    @Transactional(readOnly = false)
    public void remove(Index index) {
        Index tempIndex = getEntityManager().getReference(Index.class,
                index.getIndexid());
        getEntityManager().remove(tempIndex);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Index> findByName(String name) {
        String queryString = "from Index where name=:name";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("name", name);

        return query.getResultList();
    }

}
